package mapEditor.view.resItemEdit
{
	import asset.mapEditorItemPropertyPanelViewAsset;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mapEditor.MapEditorManager;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.view.PropertyItemView;
	import mapEditor.vo.ResItemItemVO;
	import mapEditor.vo.ResItemVO;

	public class ItemPropertyView extends mapEditorItemPropertyPanelViewAsset
	{
		private var _itemList:Vector.<PropertyItemView>=new Vector.<PropertyItemView>();
		private var _resItemVO:ResItemVO;
		private var _resItemItemVO:ResItemItemVO;		
		/**
		 *属性面板类型:1=项属性;2=项资源属性 
		 */		
		private var _type:int=1;
		
		public function ItemPropertyView()
		{
			setView();
			addEvent();
		}
		
		private function setView():void
		{
			tabItem.gotoAndStop(2);
			tabItemAsset.gotoAndStop(2);
			switch(_type)
			{
				case 1:
					tabItem.gotoAndStop(1);
					break;
				case 2:
					tabItemAsset.gotoAndStop(1);
					break;
			}
		}
		
		private function addEvent():void
		{
			MapEditorManager.instance.addEventListener(MapEditorEvent.RES_ITEM_ITEM_SELECTED, __onItemSelected);
			MapEditorManager.instance.addEventListener(MapEditorEvent.RES_ITEM_ITEM_ASSET_SELECTED, __onItemAssetSelected);
			
			pnlSet.chkVisibleGrid.addEventListener(MouseEvent.CLICK, __onChkClick);
			pnlSet.chkVisibleAsset.addEventListener(MouseEvent.CLICK, __onChkClick);
		}
		
		private function removeEvent():void
		{
			MapEditorManager.instance.removeEventListener(MapEditorEvent.RES_ITEM_ITEM_SELECTED, __onItemSelected);
			MapEditorManager.instance.removeEventListener(MapEditorEvent.RES_ITEM_ITEM_ASSET_SELECTED, __onItemAssetSelected);
			pnlSet.chkVisibleGrid.removeEventListener(MouseEvent.CLICK, __onChkClick);
			pnlSet.chkVisibleAsset.removeEventListener(MouseEvent.CLICK, __onChkClick);
		}
		
		private function __onChkClick(evt:MouseEvent):void
		{
			switch(evt.currentTarget)
			{
				case pnlSet.chkVisibleGrid://显示网络
					MapEditorManager.instance.mapEditorModel.editAreaVO.visibleGrid=pnlSet.chkVisibleGrid.selected;
					MapEditorManager.instance.mapEditorModel.editAreaVO.change();
					break;
				case pnlSet.chkVisibleAsset://显示物件
					MapEditorManager.instance.mapEditorModel.editAreaVO.visibleAsset=pnlSet.chkVisibleAsset.selected;
					MapEditorManager.instance.mapEditorModel.editAreaVO.change();
					break;
			}
		}
		
		private function __onItemSelected(evt:MapEditorEvent):void
		{
			if(_resItemVO)
			{
				_resItemVO.removeEventListener(MapEditorEvent.RES_ITEM_PROPERTY_CHANGE, __onPropertyChange);
				_resItemVO=null;
			}
			
			_resItemVO=evt.date as ResItemVO;
			_resItemVO.addEventListener(MapEditorEvent.RES_ITEM_PROPERTY_CHANGE, __onPropertyChange);
			_type=1;
			update();
		}
		
		private function __onItemAssetSelected(evt:MapEditorEvent):void
		{
			if(_resItemItemVO)_resItemItemVO.removeEventListener(Event.CHANGE, __onResItemItemVOPropertyChange);
			_resItemItemVO=evt.date as ResItemItemVO;
			if(_resItemItemVO)
			{
				_resItemItemVO.addEventListener(Event.CHANGE, __onResItemItemVOPropertyChange);
				_type=2;
			}
			else
			{
				_type=1;
			}
			update();
		}
		
		private function __onResItemItemVOPropertyChange(evt:Event):void
		{
			update();
		}		
		
		private function __onPropertyChange(evt:MapEditorEvent):void
		{
			update();
		}
		
		private function update():void
		{
			tabItem.gotoAndStop(2);
			tabItemAsset.gotoAndStop(2);
			switch(_type)
			{
				case 1:
					tabItem.gotoAndStop(1);
					setRes();
					break;
				case 2:
					tabItemAsset.gotoAndStop(1);
					setResAsset();
					break;
			}
			pnlSet.y=posPropertyList.y+posPropertyList.height+5;
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		private function addItem(item:PropertyItemView):void
		{
			item.x=1;
			item.y=_itemList.length*item.height;
			posPropertyList.addChild(item);
			_itemList.push(item);
		}
		
		private function clearAllItem():void
		{
			while(_itemList && _itemList.length>0)
			{
				var propertyItemView:PropertyItemView=_itemList.shift();
				propertyItemView.removeEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
				propertyItemView.dispose();
				propertyItemView=null;
			}
		}
		
		private function setResAsset():void
		{
			clearAllItem();
			if(!_resItemItemVO)return;
			var propertyItemView:PropertyItemView=new PropertyItemView("ID", _resItemItemVO.assetID.toString(), 0, false);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("X", _resItemItemVO.x.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			propertyItemView.gotoAndStop(2);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("Y", _resItemItemVO.y.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("Z", _resItemItemVO.z.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			propertyItemView.gotoAndStop(2);
			addItem(propertyItemView);
			
			posPropertyList.graphics.clear();
			posPropertyList.graphics.lineStyle(1, 0x6e6e6e);
			posPropertyList.graphics.moveTo(0, 0);
			posPropertyList.graphics.lineTo(0, posPropertyList.height);
			posPropertyList.graphics.lineTo(174, posPropertyList.height-1);
			posPropertyList.graphics.lineTo(174, 0);
			posPropertyList.graphics.endFill();
		}
		
		private function setRes():void
		{
			clearAllItem();
			if(!_resItemVO)return;
			
			var propertyItemView:PropertyItemView=new PropertyItemView("ID", _resItemVO.id.toString(), 0, false);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("名称", _resItemVO.name, 0);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			propertyItemView.gotoAndStop(2);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("行大小", _resItemVO.gridRow.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("列大小", _resItemVO.gridCols.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			propertyItemView.gotoAndStop(2);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("关键词", _resItemVO.keywords, 0);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			addItem(propertyItemView);
			
			posPropertyList.graphics.clear();
			posPropertyList.graphics.lineStyle(1, 0x6e6e6e);
			posPropertyList.graphics.moveTo(0, 0);
			posPropertyList.graphics.lineTo(0, posPropertyList.height);
			posPropertyList.graphics.lineTo(174, posPropertyList.height-1);
			posPropertyList.graphics.lineTo(174, 0);
			posPropertyList.graphics.endFill();
		}
		
		private function __onPropertyItemChange(evt:MapEditorEvent):void
		{
			var propertyItemView:PropertyItemView=evt.currentTarget as PropertyItemView;
			switch(_type)
			{
				case 1:
					switch(propertyItemView.propertyName)
					{
						case "名称":
							_resItemVO.name=propertyItemView.value;
							break;
						case "行大小":
							_resItemVO.gridRow=int(propertyItemView.value);
							break;
						case "列大小":
							_resItemVO.gridCols=int(propertyItemView.value);
							break;
						case "关键词":
							_resItemVO.keywords=propertyItemView.value;
							break;
					}
					_resItemVO.change();
					break;
				case 2:
					switch(propertyItemView.propertyName)
					{
						case "X":
							_resItemItemVO.x=int(propertyItemView.value);
							break;
						case "Y":
							_resItemItemVO.y=int(propertyItemView.value);
							break;
						case "Z":
							_resItemItemVO.z=int(propertyItemView.value);
							break;
					}
					_resItemItemVO.change();
					break;
			}
		}
		
		public function dispose():void
		{
			removeEvent();
			if(_resItemItemVO)
			{
				_resItemItemVO.removeEventListener(Event.CHANGE, __onResItemItemVOPropertyChange);
				_resItemItemVO=null;
			}
			clearAllItem();
			_itemList=null;
			if(parent)parent.removeChild(this);
		}
	}
}